package top.doe.spark.netty

import top.doe.spark.network.server.TransportServer
import top.doe.spark.network.{RpcHandler, TransportContext}
import top.doe.spark.rpc.{RpcEndpoint, RpcEndpointRef, RpcEnv}
import top.doe.spark.util.RpcAddress




class NettyRpcEnv(val numUsableCores: Int) extends RpcEnv {

  var server: TransportServer = _
  lazy val address: RpcAddress = if(server != null) RpcAddress(server.host, server.port) else null

  private val dispatcher = new Dispatcher(this, numUsableCores)
  private val transportContext = new TransportContext(new RpcHandler)
  override def setupEndpoint(name: String, endpoint: RpcEndpoint): RpcEndpointRef = {
    dispatcher.registerRpcEndpoint(name, endpoint)
  }

  def startServer(bindAddress: String, port: Int) = {
    server = transportContext.createServer(bindAddress, port)
  }

  override def endpointRef(endpoint: RpcEndpoint): RpcEndpointRef = ???

  override def setupEndpointRef(address: RpcAddress, endpointName: String): RpcEndpointRef = ???
}
